package vip.kqsy.cloud.blog.server.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.kqsy.cloud.blog.server.pojo.dto.BlogInfoAddDTO;
import vip.kqsy.cloud.blog.server.pojo.dto.BlogInfoUpdateDTO;
import vip.kqsy.cloud.blog.server.pojo.dto.ListBlogInfoDTO;
import vip.kqsy.cloud.blog.server.pojo.dto.PageBlogInfoDTO;
import vip.kqsy.cloud.blog.server.pojo.vo.*;
import vip.kqsy.cloud.blog.server.service.BlogInfoService;
import vip.kqsy.cloud.core.security.user.KqsyUserContext;
import vip.kqsy.cloud.core.web.res.R;
import vip.kqsy.cloud.core.web.res.ResPage;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 博客表 前端控制器
 * </p>
 *
 * @author cenjunjie
 * @since 2023-05-03
 */
@RestController
@RequestMapping("/blogInfo")
@RequiredArgsConstructor
@Valid
@Api(tags = "博客")
public class BlogInfoController {

    private final BlogInfoService blogInfoService;

    @GetMapping("page")
    @ApiOperation("分页查询博客")
    public R<ResPage<PageBlogInfoVO>> page(PageBlogInfoDTO dto) {
        return R.ok(blogInfoService.page(dto));
    }

    @PostMapping("add")
    @ApiOperation("新增博客")
    public R<Void> add(@Valid BlogInfoAddDTO dto) {
        blogInfoService.add(dto);
        return R.ok();
    }

    @GetMapping("detail")
    @ApiOperation("博客详情")
    public R<BlogInfoDetailVO> detail(@Parameter(description = "博客ID") @NotNull Long id) {
        BlogInfoDetailVO detail = blogInfoService.detail(id);
        return R.ok(detail);
    }

    @PostMapping("update")
    @ApiOperation("更新博客")
    public R<Void> update(@Valid BlogInfoUpdateDTO dto) {
        blogInfoService.update(dto);
        return R.ok();
    }

    @GetMapping("list")
    @ApiOperation("查询博客列表")
    public R<ResPage<ListBlogInfoVO>> list(@Valid ListBlogInfoDTO dto) {
        ResPage<ListBlogInfoVO> list = blogInfoService.list(dto);
        return R.ok(list);
    }


    @GetMapping("view")
    @ApiOperation("博客详情")
    public R<BlogInfoViewVO> view(@Parameter(description = "博客ID") @NotNull Long id) {
        BlogInfoViewVO vo = blogInfoService.view(id, KqsyUserContext.getUser().getId());
        return R.ok(vo);
    }


    @GetMapping("archive")
    @ApiOperation("归档")
    public R<List<ArchiveVO>> archive() {
        List<ArchiveVO> vo = blogInfoService.archive();
        return R.ok(vo);
    }


    @GetMapping("listInteractive")
    @ApiOperation("按交互类型查询博客")
    public R<List<ListInteractiveBlogVO>> listInteractive(@Parameter(description = "类型") @NotNull Integer type) {
        List<ListInteractiveBlogVO> vo = blogInfoService.listInteractive(type, KqsyUserContext.getUser().getId());
        return R.ok(vo);
    }

}
